 <template>
  <div class="app">
    <header>
      <div class="head">
        <img src="@/assets/right (1).png" alt="" @click="go()" />
        <span>密码登录</span>
      </div>
    </header>
    <main>
      <div class="login">
        <div class="login-top">
          <input type="text" placeholder="账号" v-model="tex" />
          <input :type="pawd" placeholder="密码" v-model="pos" />
          <input type="text" placeholder="验证码" v-model="code" />
          <img :src="imgsrc" alt="" class="code" />
          <span class="hyh" @click="hyh">换一换</span>
          <van-switch v-model="checked" class="van" @click="check()" />
        </div>
        <div class="login-bottom">
          <p>温馨提示：未注册过的账号，登录时将自动注册</p>
          <p>注册过的用户可凭账号密码登录</p>
          <p @click="log">登录</p>
        </div>
        <span class="cz">重置密码?</span>
      </div>
    </main>
  </div>
</template>
 <script>
import { getcode, getstatus } from "@/api/index.js";
import Vue from "vue";
import { Dialog } from "vant";
import { Switch } from "vant";
Vue.use(Switch);
Vue.use(Dialog);
export default {
  data() {
    return {
      tex: "",
      pos: "",
      code: "",
      imgsrc: "",
      checked: true,
      pawd: "",
    };
  },
  methods: {
    go() {
      this.$router.go(-1);
    },
    async log() {
      var h = /^1[3456789]\d{9}$/;
      if (h.test(this.tex) == false) {
        Dialog.alert({
          message: "请输入用户名",
        }).then(() => {
          // on close
        });
        return;
      }
      if (!this.pos) {
        Dialog.alert({
          message: "请输入密码",
        }).then(() => {
          // on close
        });
        return;
      }
      if (!this.code) {
        Dialog.alert({
          message: "请输入验证码",
        }).then(() => {
          // on close
        });
        return;
      }

      var res = await getstatus({
        captcha_code: this.code,
        password: this.pos,
        username: this.tex,
      });

      if (res.data.message) {
        Dialog.alert({
          message: "请输入正确信息",
        }).then(() => {
          // on close
        });
      } else {
        this.$store.commit("log", res.data);
        this.$router.push("/Mylogin");
      }
    },
    async hyh() {
      var h = await getcode();
      this.imgsrc = h.data.code;
    },
    check() {
      if (this.checked) {
        this.pawd = "password";
      } else {
        this.pawd = "text";
      }
    },
  },
  async mounted() {
    var h = await getcode();
    this.imgsrc = h.data.code;
    console.log(this.imgsrc);
  },
  computed: {},
};

</script>
 <style lang='css' scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.app {
  width: 100%;
  height: 100%;
  background: #f5f5f5;
}
header {
  width: 100%;
  height: 3rem;
  background: #3190e8;
  padding-left: 10px;
}
.head {
  width: 13rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}
.head img {
  width: 1.5rem;
  height: 1.5rem;
}
.head span {
  color: #fff;
}

main {
  flex: 1;
  padding-top: 1rem;
}
.login {
  width: 100%;
  height: 16rem;
  background: #fff;
  display: flex;
  flex-direction: column;
  position: relative;
}
.login-top {
  width: 100%;
  height: 8rem;
  display: flex;
  flex-direction: column;
  position: relative;
}
.login-top input {
  width: 100%;
  height: 2.6rem;
  outline: none;
  border: none;
  border-bottom: 1px solid #ccc;
  padding-left: 10px;
  position: relative;
}
.login-bottom {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0rem 10px;
}
.login-bottom p {
  font-size: 13px;
  color: red;
  margin-bottom: 5px;
}
.login-bottom p:nth-of-type(3) {
  font-size: 16px;
  color: #fff;
  background: #4cd964;
  height: 2.5rem;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
}
.cz {
  position: absolute;
  right: 10px;
  bottom: 0rem;
  color: #3190e8;
  font-size: 14px;
}
.code {
  position: absolute;
  right: 45px;
  top: 90px;
}
.hyh {
  position: absolute;
  right: 0px;
  top: 95px;
  font-size: 14px;
}
.van {
  position: absolute;
  right: 10px;
  top: 45px;
}
</style>